package com.jms.rabbitmq.impl;

import com.alibaba.fastjson.JSONObject;
import com.jms.rabbitmq.MessageService;
import com.jms.rabbitmq.Sender;
import com.jms.rabbitmq.constant.RabbitMQConstant;
import com.jms.vo.SendMessageCondition;
import com.jms.vo.SystemMessageBean;
import com.jms.vo.TaskMsgCondition;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;


@Service
@Transactional
@ConditionalOnProperty(name = "config.rabbitmq.enable", havingValue = "true")
public class MessageServiceImpl extends Sender<Object> implements MessageService {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());


    public void sendMessage(SendMessageCondition condition) {
        Map<String, Object> header = new HashMap(3);
        header.put("current_app", condition.getAppCode());
        header.put("current_tenant", condition.getTenantId());

        try {
            logger.info("[" + Thread.currentThread().getId() + "]发送消息：" + JSONObject.toJSONString(condition));
            logger.info("[" + Thread.currentThread().getId() + "]发送消息：statusCode");
            // 发送了，在哪里接呢？
            send(RabbitMQConstant.EXCHANGE_NAME_BPM, RabbitMQConstant.ROUTING_KEY_BPM, condition);
        } catch (Exception e) {
            logger.error("发送消息异常：", e);
        }
    }


    public void sendMessage(String exchange, String routingKey, Object object) {
        logger.info("发送消息：exchange: {},routingKey: {},Object: {}", exchange, routingKey, JSONObject.toJSONString(object));
        this.send(exchange, routingKey, object);
    }


    public void sendMessage(TaskMsgCondition object) {
        send(RabbitMQConstant.EXCHANGE_NAME_BPM, RabbitMQConstant.ROUTING_KEY_BPM, object);
    }


    public void sendMessage(SystemMessageBean bean) {
        send(RabbitMQConstant.EXCHANGE_NAME_BPM, RabbitMQConstant.ROUTING_KEY_BPM, bean);
        logger.info("------------- 发送系统消息 END -------------");

    }


}
